package com.ibm.risk.irmp.rule.contoller;


import com.ibm.risk.irmp.rule.model.SourceRuleDomain;
import com.ibm.risk.irmp.rule.service.RuleDomainService;
import com.ibm.risk.irmp.rule.service.RuleEngineService;
import com.ibm.risk.irmp.rule.service.RuleServiceImpl;
import com.ibm.risk.irmp.rule.vo.RuleContent;
import com.ibm.risk.irmp.rule.vo.RuleDomain;
import com.ibm.risk.irmp.rule.vo.RuleResult;
import com.ibm.risk.irmp.rule.vo.RuleSumResult;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/rules")
public class RuleController implements RuleEngineService, RuleDomainService {

    @Autowired
    RuleServiceImpl ruleService;

    @Override
    @PostMapping("")
    public void addOrUpdateDroolsRule(@RequestBody RuleContent droolsRule) {
        ruleService.addOrUpdateDroolsRule(droolsRule);
    }

    @Override
    @DeleteMapping("/{code}")
    public void deleteDroolsRule(@PathVariable String code) {
        ruleService.deleteDroolsRule(code);
    }

    @Override
    @GetMapping("/{code}")
    public RuleContent getRuleContent(@PathVariable String code) {
        return ruleService.getRuleContent(code);
    }

    @Override
    @PostMapping("/gen")
    public String genRuleScript(@RequestBody SourceRuleDomain sourceRuleDomain) throws Exception {
        return ruleService.genRuleScript(sourceRuleDomain);
    }

    @Override
    @PutMapping("/deploy")
    public void deployRuleDomain(@RequestBody SourceRuleDomain sourceRuleDomain) throws Exception {
        ruleService.deployRuleDomain(sourceRuleDomain);
    }

    @Override
    @GetMapping("")
    public List<RuleContent> getAllRules() {
        return ruleService.getAllRules();
    }

    @Override
    @GetMapping("/domain")
    public List<RuleDomain> getAllDomain() {
        return ruleService.getAllDomain();
    }

    @Override
    @GetMapping("/active")
    public List<RuleContent> getActiveRules() {
        return ruleService.getActiveRules();
    }

    @Operation(summary = "fire rules with specified domain, read/write data automatically with importer/exporter defined by domain.")
    @Override
    @PostMapping("/fire/domains/{domain}")
    public RuleSumResult fireRule(@PathVariable String domain, @RequestBody Map<String, Object> params) {
        return ruleService.fireRule(domain, params);
    }

    @Operation(summary = "Execute rules with provided data list.")
    @Override
    @PostMapping("/execute/domains/{domain}")
    public RuleResult<?> executeRules(@PathVariable String domain, @RequestBody List<Map<String, Object>> dataList) throws Exception {
        return ruleService.executeRules(domain, dataList);
    }
}
